package org.mao.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.mao.util.HivWebSocketHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("test")
public class TestController {

	private final static Logger logger = Logger.getLogger(TestController.class);

	private final static AtomicInteger aint = new AtomicInteger();

	public final static Set<String> nickNameSet = new HashSet<String>();

	@RequestMapping(value = "chart")
	public String chart(HttpServletRequest request, HttpServletResponse response) {
		return "chart/chart";
	}

	@RequestMapping(value = "check")
	@ResponseBody
	public JSONObject check(HttpServletRequest request, HttpServletResponse response) {

		JSONObject json = new JSONObject();
		try {
			String nickName = (String) request.getSession().getAttribute("nickName");
			if (StringUtils.isNotBlank(nickName)) {
				if (nickNameSet.contains(nickName)) {
					json.put("nickName", nickName);
				}
			}
		} catch (Exception e) {
			logger.error("", e);
		}
		return json;
	}

	@RequestMapping(value = "register")
	@ResponseBody
	public JSONObject register(HttpServletRequest request, HttpServletResponse response, String nickName) {

		JSONObject json = new JSONObject();
		try {
			if (StringUtils.isBlank(nickName)) {
				json.put("msg", "注册失败，请输入正确的昵称");
			} else if (nickNameSet.contains(nickName)) {
				json.put("msg", "注册失败，昵称已存在");
			} else {
				request.getSession().setAttribute("nickName", nickName);
				nickNameSet.add(nickName);
			}
		} catch (Exception e) {
			logger.error("", e);
			json.put("msg", e.toString());
		}
		return json;
	}

	@RequestMapping(value = "unregister")
	@ResponseBody
	public JSONObject unregister(HttpServletRequest request, HttpServletResponse response, String nickName) {

		JSONObject json = new JSONObject();
		try {
			HivWebSocketHandler.sessionMap.remove(nickName);
			request.getSession().removeAttribute("nickName");
			nickNameSet.remove(nickName);
		} catch (Exception e) {
			logger.error("", e);
			json.put("msg", e.toString());
		}
		return json;
	}

	@RequestMapping(value = "highRequest")
	@ResponseBody
	public JSONObject highRequest(HttpServletRequest request, HttpServletResponse response) {

		JSONObject json = new JSONObject();
		try {
			json.put("msg", "success");
			// Map<Integer, Integer> map = new HashMap<Integer, Integer>();
			// for (int i = 0; i < Integer.MAX_VALUE; i++) {
			// map.put(i, i);
			// }
			logger.info("request: " + aint.getAndIncrement());
		} catch (Exception e) {
			logger.error("", e);
			json.put("msg", e.toString());
		}
		return json;
	}

}
